package com.hanxu.mallproduct.service.impl;

import com.hanxu.mallproduct.dao.AttrAttrgroupRelationDao;
import com.hanxu.mallproduct.dao.AttrDao;
import com.hanxu.mallproduct.entity.AttrAttrgroupRelationEntity;
import com.hanxu.mallproduct.entity.AttrEntity;
import com.hanxu.mallproduct.vo.AttrGroupRelationVo;
import com.hanxu.mallproduct.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanxu.common.utils.PageUtils;
import com.hanxu.common.utils.Query;

import com.hanxu.mallproduct.dao.AttrGroupDao;
import com.hanxu.mallproduct.entity.AttrGroupEntity;
import com.hanxu.mallproduct.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) throws UnsupportedEncodingException {
        String key=(String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (categoryId != 0) {
            wrapper.eq("catelog_id", categoryId);
        }
        if (StringUtils.hasText(key)) {
            String tmpKey=URLDecoder.decode(key, "UTF-8");
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", tmpKey).or().like("attr_group_name", tmpKey);
            });
        }
        return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params), wrapper));
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 查询分组信息
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = new ArrayList<>();
        // 查询所有属性
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
            List<AttrEntity> attrEntities=new ArrayList<>();
            List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupEntity.getAttrGroupId()));
            // 查询每一个分组对应的属性
            for (AttrAttrgroupRelationEntity attrAttrgroupRelationEntity : entities) {
                if(attrAttrgroupRelationEntity.getAttrId()!=null){
                    attrEntities.add(attrDao.selectById(attrAttrgroupRelationEntity.getAttrId()));
                }
            }
            attrGroupWithAttrsVo.setAttrs(attrEntities);
            attrGroupWithAttrsVos.add(attrGroupWithAttrsVo);
        }
        return attrGroupWithAttrsVos;
    }


}